"use strict";

const path = require("node:path");
const aliasKit = require("alias-kit");
const { Project, SyntaxKind } = require("ts-morph");
// rootLevel
const ts = require("typescript");

const project = new Project();
const sourceFile = project.addSourceFileAtPath(
  aliasKit.pwd2(path.join(__dirname, "./example.ts")),
);

// 假设我们有这样的代码：
// const config = {
//   name: "oldValue",
//   version: "1.0.0"
// };

// 找到所有的对象字面量
const objectLiterals = sourceFile.getDescendantsOfKind(
  SyntaxKind.ObjectLiteralExpression,
);

for (const objLiteral of objectLiterals) {
  // 找到特定的属性（例如 name 属性）
  const nameProperty = objLiteral.getProperty("name");

  if (nameProperty && nameProperty.isKind(SyntaxKind.PropertyAssignment)) {
    const initializer = nameProperty.getInitializer();

    // 确保初始值是字符串字面量
    if (initializer && initializer.isKind(SyntaxKind.StringLiteral)) {
      // 修改字符串值
      initializer.replaceWithText((write) => write.quote("newValue"));

      console.log("修改成功！");
    }
  }
}

// 保存修改
sourceFile.saveSync();
